home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Wave / WavesWorld / Source / IBPalettes / WW3DKit / WW3DSquishySphere.m < prev    next >
Encoding:
Text File  |  1995-03-25  |  6.2 KB  |  250 lines

  1.  
  2. #import "WW3DSquishySphere.h"
  3. #import "RIBTransformBegin.h"
  4. #import "RIBScale.h"
  5. #import "RIBSphere.h"
  6. #import "RIBTransformEnd.h"
  7. #import "WWSample.h"
  8. #import "WWEveParser.h"
  9. #import "WW3DAttributeState.h"
  10.  
  11. @implementation WW3DSquishySphere
  12.  
  13. + initialize { return [WW3DSquishySphere setVersion:1], self; }
  14.  
  15. - init
  16. {
  17.   [super init];
  18.  
  19.   radius = 1.0;
  20.   zMin = -1.0;
  21.   zMax =  1.0;
  22.   xScale = yScale = zScale = 1.0;
  23.   
  24.   return self;
  25. }
  26.  
  27.  
  28. - awake
  29. {
  30.     [super awake];
  31.     return self;
  32. }
  33.  
  34.  
  35. - setYScale:(RtFloat)newYScale
  36. {
  37.    yScale = newYScale;
  38.    xScale = zScale = 1./pow((double)yScale, (double)(1./3.));
  39.    return self;
  40. }
  41.  
  42. - setRadius:(RtFloat)newRadius 
  43.      zMin:(RtFloat)newZMin zMax:(RtFloat)newZMax 
  44.      thetaMax:(RtFloat)newThetaMax yScale:(RtFloat)newYScale
  45.      n:(int)newN tokens:(RtToken *)newTokens parms:(RtPointer *)newParms archiveVector:(char **)newArchiveVector
  46.      printfTypeVector:(int *)newPrintfTypeVector printfNVector:(int *)newPrintfNVector
  47. {  
  48.    id  sphereObj;
  49.  
  50.  
  51.    radius = newRadius;
  52.    zMin = newZMin;
  53.    zMax = newZMax;
  54.    thetaMax = newThetaMax;
  55.    [self setYScale:newYScale];
  56.  
  57.    [ribCommandList empty];
  58.  
  59.    sphereObj = [[RIBSphere alloc] init]; 
  60.    [sphereObj setRadius:radius zMin:zMin zMax:zMax thetaMax:thetaMax
  61.                     n:newN tokens:newTokens parms:newParms 
  62.                     archiveVector:newArchiveVector 
  63.                     printfTypeVector:newPrintfTypeVector printfNVector:newPrintfNVector];
  64.    [sphereObj setMyShape:myShape];
  65.  
  66.    [ribCommandList insertObject:sphereObj at:0];
  67.    [ribCommandList insertObject:[[[[RIBScale alloc] init] setSX:xScale sy:yScale sz:zScale] setMyShape:myShape] at:0];
  68.    [ribCommandList insertObject:[[[RIBTransformBegin alloc] init] setMyShape:myShape] at:0];
  69.    [ribCommandList addObject:[[[RIBTransformEnd alloc] init] setMyShape:myShape]];
  70.  
  71.    dirtyBoundingBox = TRUE;
  72.   
  73.    return self;
  74. }
  75.  
  76. - (RtFloat)radius { return radius; } 
  77. - (RtFloat)yScale { return yScale; }
  78. - (RtFloat)zMin { return zMin; }
  79. - (RtFloat)zMax { return zMax; }
  80. - (RtFloat)thetaMax { return thetaMax; }
  81.  
  82. - _setRadius:(RtFloat)newRadius 
  83.      zMin:(RtFloat)newZMin zMax:(RtFloat)newZMax 
  84.      thetaMax:(RtFloat)newThetaMax yScale:(RtFloat)newYScale
  85. {  
  86.    radius = newRadius;
  87.    zMin = newZMin;
  88.    zMax = newZMax;
  89.    thetaMax = newThetaMax;
  90.    [self setYScale:newYScale];
  91.  
  92.    dirtyBoundingBox = TRUE;
  93.   
  94.    return self;
  95. }
  96.  
  97. // WavesWorld archiving:
  98. // writeEve:(NXStream *)stream
  99. // writeScene:(NXStream *)stream
  100.  
  101. - lerpWith:b by:(float)uValue
  102. {
  103.    id  newMe;  
  104.  
  105.  
  106.    if (([self class] != [b class]) || (uValue <= 0.0))
  107.    {  return self;
  108.    }
  109.  
  110.    if (uValue >= 1.0)
  111.    {  return b;
  112.    }
  113.  
  114.    newMe = [super lerpWith:b by:uValue]; // this will copy the RIBCommandList
  115.    [newMe _setRadius:([self radius] + (([b radius] - [self radius]) * uValue))
  116.               zMin:([self zMin] + (([b zMin] - [self zMin]) * uValue))
  117.               zMax:([self zMax] + (([b zMax] - [self zMax]) * uValue))
  118.               thetaMax:([self thetaMax] + (([b thetaMax] - [self thetaMax]) * uValue))
  119.               yScale:([self yScale] + (([b yScale] - [self yScale]) * uValue))];
  120.  
  121.    return newMe;
  122. }
  123.  
  124. - lerpSelfWith:b by:(float)uValue
  125. {
  126.    if (([self class] != [b class]) || (uValue <= 0.0))
  127.    {  return self;
  128.    }
  129.  
  130.    if (uValue >= 1.0)
  131.    {  return b;
  132.    }
  133.  
  134.    [super lerpWith:b by:uValue]; // this will copy the RIBCommandList
  135.    [self _setRadius:([self radius] + (([b radius] - [self radius]) * uValue))
  136.               zMin:([self zMin] + (([b zMin] - [self zMin]) * uValue))
  137.               zMax:([self zMax] + (([b zMax] - [self zMax]) * uValue))
  138.               thetaMax:([self thetaMax] + (([b thetaMax] - [self thetaMax]) * uValue))
  139.               yScale:([self yScale] + (([b yScale] - [self yScale]) * uValue))];
  140.  
  141.    return self;
  142. }
  143.  
  144. - writeEve:(NXStream *)stream atTabLevel:(int)tab
  145. {
  146.     int  i;
  147.     
  148.     for (i = 0; i < tab; i++) {
  149.     NXPrintf(stream, "\t");
  150.     }
  151.     NXPrintf(stream, "WW3DSquishySphere %f %f %f %f", radius, zMin, zMax, thetaMax, yScale);
  152.     return self;
  153. }
  154.  
  155. - writeScene:(NXStream *)stream atTabLevel:(int)tab
  156. {
  157.     int  i;
  158.     
  159.     for (i = 0; i < tab; i++) {
  160.     NXPrintf(stream, "\t");
  161.     }
  162.     NXPrintf(stream, "WW3DSquishySphere %f %f %f %f", radius, zMin, zMax, thetaMax, yScale);
  163.     return self;
  164. }
  165.  
  166. - write3DTextScene:(NXStream *)stream atTabLevel:(int)tab index:(int)index time:(float)time until:(float)lastTime
  167. {
  168.    int  i;
  169.  
  170.  
  171.    for (i = 0; i < tab; i++)
  172.    {  NXPrintf(stream, "\t");
  173.    }
  174.  
  175.    NXPrintf(stream, "startShape %s; ", [[self class] name]);
  176.    // need tab
  177.    // need index (position in current list)
  178.    NXPrintf(stream, 
  179.         "EveCmd {Translate [expr { %d * $__text__(tabLength)}] [expr {$__text__(spacingFactor) * %d * $__text__(spacing) * $__text__(fontSize)}] 0 };\n", 
  180.             tab, index); 
  181.    NXPrintf(stream, "  EveCmd {WW3DText $__text__(fontName) $__text__(fontSize) {");
  182.    [self writeEve:stream atTabLevel:tab];
  183.    NXPrintf(stream, "} left;}\n");
  184.   NXPrintf(stream, "endShape;\n");
  185.  
  186.   return self;
  187. }
  188.  
  189. - writeInventorAtTime:(float)currentTime to:(NXStream *)stream atTabLevel:(int)tab
  190. {
  191.    int  i;
  192.  
  193.  
  194.    for (i = 0; i < tab; i++)
  195.    {  NXPrintf(stream, "\t");
  196.    }
  197.    NXPrintf(stream, "# ");
  198.    [self writeEve:stream atTabLevel:tab];
  199.    NXPrintf(stream, "\n");
  200.  
  201.    return [ribCommandList writeInventorAtTime:currentTime to:stream atTabLevel:tab];
  202. }
  203.  
  204. #define typeVector "ffff"
  205. #define typeValues &radius, &zMin, &zMax, &yScale
  206.  
  207. - read:(NXTypedStream *)stream 
  208. {
  209.     int version;
  210.     
  211.     [super read:stream];
  212.     version = NXTypedStreamClassVersion(stream,"WW3DSquishySphere");
  213.     if (version == 0) NXReadTypes(stream, "i", &version), version = 1;
  214.     if (version == 1)
  215.     {  NXReadTypes(stream, typeVector, typeValues);
  216.        xScale = zScale = 1./sqrt((double)yScale);
  217.     }
  218.  
  219.     return self;
  220. }
  221.  
  222. - write:(NXTypedStream *)stream 
  223. {
  224.     [super write:stream];
  225.     NXWriteTypes(stream, typeVector, typeValues);
  226.     return self;
  227. }
  228.  
  229. - (BOOL)theSameAs:otherRIBCommand
  230. {
  231.   if (radius != [otherRIBCommand radius])
  232.   {  return NO;
  233.   }
  234.   if (yScale != [otherRIBCommand yScale])
  235.   {  return NO;
  236.   }
  237.   if (zMin != [otherRIBCommand zMin])
  238.   {  return NO;
  239.   }
  240.   if (zMax != [otherRIBCommand zMax])
  241.   {  return NO;
  242.   }
  243.   if (thetaMax != [otherRIBCommand thetaMax])
  244.   {  return NO;
  245.   }
  246.   return [super theSameAs:otherRIBCommand];
  247. }
  248.  
  249. @end
  250.